<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>第 4 章 R语言开发 | 小蓝哥的知识荒原</title>
<meta name="author" content="李详">
<meta name="description" content="4.0.1 一些资源 Building R packages with devtools and usethis | RStudio Using RMarkdown to Create Reproducible Documents (Oct 26, 2021) Presenter: Mine Çetinkaya-Rundel  4.1 一些Tips 将数据框作为提示信息进行输出：...">
<meta name="generator" content="bookdown 0.24 with bs4_book()">
<meta property="og:title" content="第 4 章 R语言开发 | 小蓝哥的知识荒原">
<meta property="og:type" content="book">
<meta property="og:image" content="https://raw.githubusercontent.com/DivadNojnarg/outstanding-shiny-ui/master/images/intro/crc-press-cover.svg">
<meta property="og:description" content="4.0.1 一些资源 Building R packages with devtools and usethis | RStudio Using RMarkdown to Create Reproducible Documents (Oct 26, 2021) Presenter: Mine Çetinkaya-Rundel  4.1 一些Tips 将数据框作为提示信息进行输出：...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第 4 章 R语言开发 | 小蓝哥的知识荒原">
<meta name="twitter:description" content="4.0.1 一些资源 Building R packages with devtools and usethis | RStudio Using RMarkdown to Create Reproducible Documents (Oct 26, 2021) Presenter: Mine Çetinkaya-Rundel  4.1 一些Tips 将数据框作为提示信息进行输出：...">
<meta name="twitter:image" content="https://raw.githubusercontent.com/DivadNojnarg/outstanding-shiny-ui/master/images/intro/crc-press-cover.svg">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/header-attrs-2.11/header-attrs.js"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.3.1/transition.js"></script><script src="libs/bs3compat-0.3.1/tabs.js"></script><script src="libs/bs3compat-0.3.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><link rel="stylesheet" href="css/style.css">
</head>
<body data-spy="scroll" data-target="#toc">

<div class="container-fluid">
<div class="row">
  <header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>

    <div class="d-flex align-items-start justify-content-between">
      <h1>
        <a href="index.html" title="">小蓝哥的知识荒原</a>
      </h1>
      <button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
    </div>

    <div id="main-nav" class="collapse-lg">
      <form role="search">
        <input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>

      <nav aria-label="Table of contents"><h2>Table of contents</h2>
        <ul class="book-toc list-unstyled">
<li><a class="" href="index.html">简介</a></li>
<li class="book-part">Part I: R</li>
<li><a class="" href="r-base.html"><span class="header-section-number">1</span> 语言基础</a></li>
<li><a class="" href="r-stat.html"><span class="header-section-number">2</span> 统计分析</a></li>
<li><a class="" href="r-vis.html"><span class="header-section-number">3</span> 数据可视化</a></li>
<li><a class="active" href="r-deve.html"><span class="header-section-number">4</span> R语言开发</a></li>
<li class="book-part">Part Ⅱ: Python</li>
<li><a class="" href="python-base.html"><span class="header-section-number">5</span> Python基础</a></li>
<li><a class="" href="python-stat.html"><span class="header-section-number">6</span> 数据统计分析</a></li>
<li><a class="" href="python-spider.html"><span class="header-section-number">7</span> Python爬虫</a></li>
<li><a class="" href="ai.html"><span class="header-section-number">8</span> 人工智能</a></li>
<li class="book-part">Part Ⅲ: 生物信息学</li>
<li><a class="" href="bio-base.html"><span class="header-section-number">9</span> 基础知识</a></li>
<li><a class="" href="bio-env.html"><span class="header-section-number">10</span> 环境搭建</a></li>
<li><a class="" href="genomics.html"><span class="header-section-number">11</span> 基因组学</a></li>
<li><a class="" href="rnaseq.html"><span class="header-section-number">12</span> RNA-Seq</a></li>
<li><a class="" href="meta.html"><span class="header-section-number">13</span> 代谢组学</a></li>
<li><a class="" href="pro.html"><span class="header-section-number">14</span> 蛋白质组</a></li>
<li><a class="" href="multi.html"><span class="header-section-number">15</span> 多组学</a></li>
<li class="book-part">Part Ⅳ: 文献阅读</li>
<li><a class="" href="patho.html"><span class="header-section-number">16</span> 植物病理学</a></li>
<li><a class="" href="liter-genomics.html"><span class="header-section-number">17</span> 基因组学</a></li>
<li><a class="" href="liter-rnaseq.html"><span class="header-section-number">18</span> RNA-Seq</a></li>
<li><a class="" href="liter-meta.html"><span class="header-section-number">19</span> 代谢组学</a></li>
<li><a class="" href="liter-pro.html"><span class="header-section-number">20</span> 蛋白质组</a></li>
<li class="book-part">Part Ⅴ: 文章发表</li>
<li><a class="" href="myarticle.html"><span class="header-section-number">21</span> 文章发表</a></li>
<li class="book-part">Part Ⅵ: 上课笔记</li>
<li><a class="" href="class.html"><span class="header-section-number">22</span> 上课笔记</a></li>
<li class="book-part">Part Ⅶ: 其他</li>
<li><a class="" href="other.html"><span class="header-section-number">23</span> 其他笔记</a></li>
<li><a class="" href="references.html">References</a></li>
</ul>

        <div class="book-extra">
          <p><a id="book-repo" href="https://github.com/lixiang117423/book4xiang">View book source <i class="fab fa-github"></i></a></p>
        </div>
      </nav>
</div>
  </header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="r-deve" class="section level1" number="4">
<h1>
<span class="header-section-number">第 4 章</span> R语言开发<a class="anchor" aria-label="anchor" href="#r-deve"><i class="fas fa-link"></i></a>
</h1>
<div id="一些资源" class="section level3" number="4.0.1">
<h3>
<span class="header-section-number">4.0.1</span> 一些资源<a class="anchor" aria-label="anchor" href="#%E4%B8%80%E4%BA%9B%E8%B5%84%E6%BA%90"><i class="fas fa-link"></i></a>
</h3>
<ul>
<li><a href="https://colorado.rstudio.com/rsc/pkg-building/#1">Building R packages with devtools and usethis | RStudio</a></li>
<li><a href="https://www.youtube.com/watch?v=z2a80FnLFfE&amp;t=7s">Using RMarkdown to Create Reproducible Documents (Oct 26, 2021) Presenter: Mine Çetinkaya-Rundel</a></li>
</ul>
</div>
<div id="一些tips" class="section level2" number="4.1">
<h2>
<span class="header-section-number">4.1</span> 一些Tips<a class="anchor" aria-label="anchor" href="#%E4%B8%80%E4%BA%9Btips"><i class="fas fa-link"></i></a>
</h2>
<ul>
<li>将数据框作为提示信息进行输出：</li>
</ul>
<div class="sourceCode" id="cb93"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">design.demo</span> <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span>N <span class="op">=</span> <span class="fl">1</span><span class="op">:</span><span class="fl">12</span>,
                        Treatment <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"CK"</span>, <span class="st">"T1"</span>, <span class="st">"T2"</span><span class="op">)</span>,each  <span class="op">=</span><span class="fl">4</span><span class="op">)</span>,
                        Gene <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"gene1"</span>, <span class="st">"gene2"</span>, <span class="st">"gene3"</span>, <span class="st">"gene4"</span><span class="op">)</span>, <span class="fl">3</span><span class="op">)</span><span class="op">)</span>

<span class="kw"><a href="https://rdrr.io/r/base/stop.html">stop</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Design matrix should be input! Like..."</span>,
             <span class="fu"><a href="https://rdrr.io/r/utils/capture.output.html">capture.output</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">design.demo</span>, row.names <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>,
           collapse <span class="op">=</span> <span class="st">"\n"</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<ul>
<li>Linux指定R镜像，加速下载安装：</li>
</ul>
<div class="sourceCode" id="cb94"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/options.html">options</a></span> <span class="op">(</span>repos<span class="op">=</span><span class="fu"><a href="https://rdrr.io/r/base/structure.html">structure</a></span> <span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span> <span class="op">(</span>CRAN<span class="op">=</span><span class="st">"https://mirrors.tuna.tsinghua.edu.cn/CRAN/"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/options.html">options</a></span> <span class="op">(</span>BioC_mirror<span class="op">=</span><span class="st">"https://mirrors.tuna.tsinghua.edu.cn/bioconductor"</span><span class="op">)</span></code></pre></div>
</div>
<div id="pac4xiang" class="section level2" number="4.2">
<h2>
<span class="header-section-number">4.2</span> R包pac4xiang的使用<a class="anchor" aria-label="anchor" href="#pac4xiang"><i class="fas fa-link"></i></a>
</h2>
<p><code>pac4xiang</code>这个R包是我自己开发的，包含了一些我自己常用的函数。包目前在<a href="https://github.com/lixiang117423/pac4xiang">GitHub</a>上，更多功能还在完善中，待进一步开发。</p>
<div id="安装" class="section level3" number="4.2.1">
<h3>
<span class="header-section-number">4.2.1</span> 安装<a class="anchor" aria-label="anchor" href="#%E5%AE%89%E8%A3%85"><i class="fas fa-link"></i></a>
</h3>
<p>在R中运行下面的代码安装<code>pac4xiang</code>。</p>
<div class="sourceCode" id="cb95"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">devtools</span><span class="fu">::</span><span class="fu"><a href="https://devtools.r-lib.org//reference/remote-reexports.html">install_github</a></span><span class="op">(</span><span class="st">'https://github.com/lixiang117423/pac4xiang'</span><span class="op">)</span></code></pre></div>
</div>
<div id="使用方法" class="section level3" number="4.2.2">
<h3>
<span class="header-section-number">4.2.2</span> 使用方法<a class="anchor" aria-label="anchor" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"><i class="fas fa-link"></i></a>
</h3>
<div id="calstandcurve" class="section level4" number="4.2.2.1">
<h4>
<span class="header-section-number">4.2.2.1</span> <code>calStandCurve</code><a class="anchor" aria-label="anchor" href="#calstandcurve"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用与计算相对定量qPCR的标曲。qPCR标曲点样参照图<a href="r-deve.html#fig:qPCR1">4.1</a>。按照我的思路和习惯，我会准备8个浓度梯度的cDNA,从高到低分别是C0到C8。C0是对应批次下多有样品cDNA的组合，然后再以一定的稀释倍数稀释得到C1，C1再以同样的倍数稀释得到C2，依此类推，直到得到C8。</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:qPCR1"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/1.png" alt="qPCR标曲点样示意图" width="585"><p class="caption">
图4.1: qPCR标曲点样示意图
</p>
</div>
<p>函数具体怎么用呢？参考下方的代码：</p>
<div class="sourceCode" id="cb96"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">df.1</span> <span class="op">&lt;-</span> <span class="fu">calStandCurve</span><span class="op">(</span>
  data <span class="op">=</span> <span class="st">"20210927下机数据/20210928lx_1.txt"</span>NA<span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"OsPOX223"</span>, <span class="st">"OsAPX1"</span>, <span class="st">"OsAPX2"</span>, <span class="st">"OsPAL"</span><span class="op">)</span>,
  rep <span class="op">=</span> <span class="fl">3</span>,
  dilution <span class="op">=</span> <span class="fl">4</span>,
  start <span class="op">=</span> <span class="fl">2</span>,
  end <span class="op">=</span> <span class="fl">6</span>,
  drop.NA <span class="op">=</span> <span class="cn">TRUE</span>,
  fig.type <span class="op">=</span> <span class="st">"pdf"</span>
<span class="op">)</span></code></pre></div>
<p>其中基因的数量和技术重复的数量乘起来必须是12；基因的顺序必须是从左到右的；输入稀释倍数后告诉程序后续的标曲建立如何取相应的对数；<code>start</code>表示从第几行开始计算，如<code>start = 2</code>表示的是从B行开始计算标曲；对应的<code>end = 6</code>表示的是到第6行，也就是F行结束，也就意味着第一行和最后两行其实是不用的。默认是保存标曲的图片的，如果不需要保存图片加上<code>save.fig = FALSE</code>即可，默认的图片格式是<code>pdf</code>格式，可以自定义。</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:qPCR2"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/3.png" alt="函数calStandCurve返回的数据框" width="293"><p class="caption">
图4.2: 函数calStandCurve返回的数据框
</p>
</div>
<p>返回的结果是个数据框（参考图<a href="r-deve.html#fig:qPCR2">4.2</a>），包含了基因名称、标曲公式、斜率、截距、R<span class="math inline">\(^2\)</span>、P值、该标曲适用的自变量范围（最大值和最小值）及计算标曲的日期等。保存的图片默认存在当前工作目录下（运行<code>getcwd()</code>查看当前R语言工作目录）。</p>
</div>
<div id="calrtqpcr" class="section level4" number="4.2.2.2">
<h4>
<span class="header-section-number">4.2.2.2</span> <code>calRTqPCR</code><a class="anchor" aria-label="anchor" href="#calrtqpcr"><i class="fas fa-link"></i></a>
</h4>
<p>该函数在<code>calStandCurve</code>函数返回的标曲结果的基础上可以计算不同处理下各基因的表达情况。样品的点样方法参考图<a href="r-deve.html#fig:qPCR3">4.3</a>。</p>
<div class="figure" style="text-align: center">
<span style="display:block;" id="fig:qPCR3"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/2.png" alt="qPCR标曲点样示意图" width="544"><p class="caption">
图4.3: qPCR标曲点样示意图
</p>
</div>
<p>计算表达量的代码如下：</p>
<div class="sourceCode" id="cb97"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">exp.1</span> <span class="op">&lt;-</span> <span class="fu">calRTqPCR</span><span class="op">(</span>
  data <span class="op">=</span> <span class="st">"20210927下机数据/20210929lx_1.txt"</span>NA<span class="op">=</span> <span class="va">df.1</span>,
  genes <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"OsPOX223"</span>, <span class="st">"OsAPX1"</span>, <span class="st">"OsAPX2"</span>, <span class="st">"OsPAL"</span><span class="op">)</span>,
  treatment <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"CK"</span>, <span class="st">"Inter"</span>, <span class="st">"Infect"</span><span class="op">)</span>
<span class="op">)</span></code></pre></div>
<p>其中的<code>data</code>就的样品的下机导出数据，<code>StandCurve</code>是函数<code>calStandCurve</code>返回的数据框，<code>gene</code>需要严格控制大小写，必须和输入函数<code>calStandCurve</code>的基因名称一样，<code>treatment</code>是指定从左到右的处理分别是什么，这个是为了后续方便进行<em><code>t</code></em>检验。</p>
</div>
<div id="getalignresults" class="section level4" number="4.2.2.3">
<h4>
<span class="header-section-number">4.2.2.3</span> <code>getAlignResults</code><a class="anchor" aria-label="anchor" href="#getalignresults"><i class="fas fa-link"></i></a>
</h4>
<p>该函数可以将软件<code>CLUSTALW</code>的输出文件变成<code>.fasta</code>格式的比对结果。
写这个函数的原因在于每次我用MEGA进行序列比对构建进化树的时候都报错，不是字符串有问题就是长度不对，索性直接用Linux系统下的<code>CLUSTALW</code>进行序列比对，然后用MEGA构建进化树，百试不爽！</p>
<div class="sourceCode" id="cb98"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">my.aln</span> <span class="op">=</span> <span class="fu">getAlignResults</span><span class="op">(</span>aln <span class="op">=</span> <span class="st">'test.aln'</span><span class="op">)</span></code></pre></div>
<p>运行完成后会在当前目录下生成一个<code>YourAlignResults.fasta</code>文件，就是<code>fasta</code>格式的比对文件，然后就可以用来构建进化树了。</p>
<ul>
<li><p><code>mult.aov</code>
该函数用于多分组数据进行方差分析。</p></li>
<li><p><code>multGroupTtest</code>
该函数用于进行多分组的<code>t</code>检验。
这个函数是用于进行多组<code>t</code>检验的。比如每次跑qPCR，会有多个处理多个目的基因，这个时候就需要关注每个目的基因在不同的处理中的表达量。用法参考下方代码：</p></li>
</ul>
<div class="sourceCode" id="cb99"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">t.test</span> <span class="op">&lt;-</span> <span class="fu">multGroupTtest</span><span class="op">(</span>
  data <span class="op">=</span> <span class="va">exp.all.final</span>,
  group1 <span class="op">=</span> <span class="st">"Gene"</span>,
  group2 <span class="op">=</span> <span class="st">"Treatment"</span>,
  CK <span class="op">=</span> <span class="st">"CK"</span>,
  value <span class="op">=</span> <span class="st">"Expression"</span>,
  level <span class="op">=</span> <span class="fl">0.95</span>
<span class="op">)</span></code></pre></div>
</div>
<div id="plot96well" class="section level4" number="4.2.2.4">
<h4>
<span class="header-section-number">4.2.2.4</span> <code>plot96well</code><a class="anchor" aria-label="anchor" href="#plot96well"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用于可视化<code>Roche</code>96孔板的<code>Cq</code>值。</p>
<div class="sourceCode" id="cb100"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">plot96well</span><span class="op">(</span>data <span class="op">=</span> <span class="st">"20210927下机数据/20210928lx_1.txt"</span>NA<span class="op">)</span>NA</code></pre></div>
</div>
<div id="plotciselement" class="section level4" number="4.2.2.5">
<h4>
<span class="header-section-number">4.2.2.5</span> <code>plotCisElement</code><a class="anchor" aria-label="anchor" href="#plotciselement"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用与可视化基因启动子上的顺式作用元件。需要输入的参数有三个：</p>
</div>
<div id="dataplantcare返回的文件" class="section level4" number="4.2.2.6">
<h4>
<span class="header-section-number">4.2.2.6</span> <code>data</code>：<code>plantCARE</code>返回的文件；<a class="anchor" aria-label="anchor" href="#dataplantcare%E8%BF%94%E5%9B%9E%E7%9A%84%E6%96%87%E4%BB%B6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="length启动子长度" class="section level4" number="4.2.2.7">
<h4>
<span class="header-section-number">4.2.2.7</span> <code>length</code>：启动子长度；<a class="anchor" aria-label="anchor" href="#length%E5%90%AF%E5%8A%A8%E5%AD%90%E9%95%BF%E5%BA%A6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="cis需要展示哪些顺式作用元件" class="section level4" number="4.2.2.8">
<h4>
<span class="header-section-number">4.2.2.8</span> <code>Cis</code>：需要展示哪些顺式作用元件。<a class="anchor" aria-label="anchor" href="#cis%E9%9C%80%E8%A6%81%E5%B1%95%E7%A4%BA%E5%93%AA%E4%BA%9B%E9%A1%BA%E5%BC%8F%E4%BD%9C%E7%94%A8%E5%85%83%E4%BB%B6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="plotgenestructure" class="section level4" number="4.2.2.9">
<h4>
<span class="header-section-number">4.2.2.9</span> <code>plotGeneStructure</code><a class="anchor" aria-label="anchor" href="#plotgenestructure"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用于可视化基因结构。输入文件为<code>.gtf</code>文件。
如果同时输入对应的进化树文件，那么就会按照进化树的顺序对基因名称进行排序，可以和<code>ggtree</code>进行联用。</p>
</div>
<div id="fasta2df" class="section level4" number="4.2.2.10">
<h4>
<span class="header-section-number">4.2.2.10</span> <code>fasta2df</code><a class="anchor" aria-label="anchor" href="#fasta2df"><i class="fas fa-link"></i></a>
</h4>
<p>这个函数主要是为了将<code>fasta</code>格式转换成R中常见的<code>data.frame</code>格式。使用很简单，输入<code>fasta</code>格式即可。</p>
</div>
<div id="df2fasta" class="section level4" number="4.2.2.11">
<h4>
<span class="header-section-number">4.2.2.11</span> <code>df2fasta</code><a class="anchor" aria-label="anchor" href="#df2fasta"><i class="fas fa-link"></i></a>
</h4>
<p>这个函数主要是将<code>data.frame</code>格式的序列文件转换成<code>fasta</code>格式。</p>
</div>
</div>
</div>
<div id="ggmotif" class="section level2" number="4.3">
<h2>
<span class="header-section-number">4.3</span> R包ggmotif的使用方法<a class="anchor" aria-label="anchor" href="#ggmotif"><i class="fas fa-link"></i></a>
</h2>
<p>激发我开发这个R包的原因是在许做基因家族分析中的<code>motif</code>分析的时候总是找不到趁手的工具去可视化相关的结果，要么是在线网站，要么是别人开发的工具，网页版工具怕哪天就跑路了，别人的工具想要个性化比较困难。那，就自己写一个吧。</p>
<p>R包下载安装：</p>
<div class="sourceCode" id="cb101"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="st">"ggmotif"</span><span class="op">)</span>

<span class="co"># 或者</span>NA<span class="fu">devtools</span><span class="fu">::</span><span class="fu"><a href="https://devtools.r-lib.org//reference/remote-reexports.html">install_github</a></span><span class="op">(</span><span class="st">"lixiang117423/ggmotif"</span><span class="op">)</span></code></pre></div>
<p><code>meme</code>输出的结果主要是<code>XML</code>格式和<code>txt</code>格式的文件，需要的结果就在这两个文件里面。</p>
<div id="从xml文件提取结果并绘图" class="section level3" number="4.3.1">
<h3>
<span class="header-section-number">4.3.1</span> 从XML文件提取结果并绘图<a class="anchor" aria-label="anchor" href="#%E4%BB%8Exml%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E7%BB%98%E5%9B%BE"><i class="fas fa-link"></i></a>
</h3>
<p><code>motif</code>在序列上的位置结果存放在<code>XML</code>格式的文件中，运行下面的代码便可以将结果转换成数据框格式并进行可视化。</p>
<div class="sourceCode" id="cb102"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.xml"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromXML</span><span class="op">(</span>XMLfile <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>
<span class="va">motif_plot</span> <span class="op">&lt;-</span> <span class="fu">motif_location</span><span class="op">(</span>data <span class="op">=</span> <span class="va">motif_extract</span><span class="op">)</span>
<span class="va">motif_plot</span></code></pre></div>
</div>
<div id="从txt文件提取结果并进行可视化" class="section level3" number="4.3.2">
<h3>
<span class="header-section-number">4.3.2</span> 从txt文件提取结果并进行可视化<a class="anchor" aria-label="anchor" href="#%E4%BB%8Etxt%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96"><i class="fas fa-link"></i></a>
</h3>
<p>每个<code>motif</code>的序列是存放在<code>txt</code>文件中的，运行下方的代码便能结果转换成数据框格式并进行绘图。</p>
<div class="sourceCode" id="cb103"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op">)</span>

<span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.txt"</span>, package <span class="op">=</span> <span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromTxt</span><span class="op">(</span>data <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>

<span class="va">motif_extract</span> <span class="op">%&gt;%</span> 
  <span class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">motif.num</span> <span class="op">==</span> <span class="st">'Motif.1'</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="va">input.seq.motif</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">ggseqlogo</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggseqlogo/man/ggseqlogo.html">ggseqlogo</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://rdrr.io/pkg/ggplot2/man/ggtheme.html">theme_classic</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
</div>
<div id="与ggtree联用" class="section level3" number="4.3.3">
<h3>
<span class="header-section-number">4.3.3</span> 与<code>ggtree</code>联用<a class="anchor" aria-label="anchor" href="#%E4%B8%8Eggtree%E8%81%94%E7%94%A8"><i class="fas fa-link"></i></a>
</h3>
<p>有时候会对序列构建进化树展示进化关系，<code>ggmotif</code>中的函数能够以输入的进化树文件为基础，把输出的<code>motif</code>在序列上的位置与进化树上的位置进行匹配，便能够与<code>ggtree</code>联用在一个图上展示进化树和<code>motif</code>在序列上的位置。</p>
<div class="sourceCode" id="cb104"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.xml"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">treepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"tree.nwk"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromXML</span><span class="op">(</span>XMLfile <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>
<span class="va">motif_plot_with_tree</span> <span class="op">&lt;-</span> <span class="fu">motif_with_tree</span><span class="op">(</span>data <span class="op">=</span> <span class="va">motif_extract</span>, tree <span class="op">=</span> <span class="va">treepath</span><span class="op">)</span>

<span class="va">p.tree</span> <span class="op">=</span> <span class="fu">ape</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">treepath</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">ggtree</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">ggtree</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p.tree</span>  <span class="op">%&gt;%</span> 
  <span class="fu">aplot</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/aplot/man/plot-insertion.html">insert_right</a></span><span class="op">(</span><span class="va">motif_plot_with_tree</span>, width <span class="op">=</span> <span class="fl">0.8</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="r-shiny开发" class="section level2" number="4.4">
<h2>
<span class="header-section-number">4.4</span> R shiny开发<a class="anchor" aria-label="anchor" href="#r-shiny%E5%BC%80%E5%8F%91"><i class="fas fa-link"></i></a>
</h2>
<div id="css学习网站" class="section level3" number="4.4.1">
<h3>
<span class="header-section-number">4.4.1</span> CSS学习网站<a class="anchor" aria-label="anchor" href="#css%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99"><i class="fas fa-link"></i></a>
</h3>
<p><a href="https://www.runoob.com/css/css-align.html">CSS菜鸟教程</a>。</p>
<p><a href="https://unleash-shiny.rinterface.com/beautify-css.html">CSS在Rshiny中的使用</a>。</p>
</div>
<div id="服务器搭建rshiny-server" class="section level3" number="4.4.2">
<h3>
<span class="header-section-number">4.4.2</span> 服务器搭建Rshiny-server<a class="anchor" aria-label="anchor" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BArshiny-server"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb105"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb105-1"><a href="r-deve.html#cb105-1" aria-hidden="true" tabindex="-1"></a><span class="co">#root 权限进入</span></span>
<span id="cb105-2"><a href="r-deve.html#cb105-2" aria-hidden="true" tabindex="-1"></a>sudo su</span>
<span id="cb105-3"><a href="r-deve.html#cb105-3" aria-hidden="true" tabindex="-1"></a>useradd shiny <span class="co"># 添加shiny用户</span></span>
<span id="cb105-4"><a href="r-deve.html#cb105-4" aria-hidden="true" tabindex="-1"></a>passwd shiny <span class="co"># 修改shiny用户的密码</span></span>
<span id="cb105-5"><a href="r-deve.html#cb105-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-6"><a href="r-deve.html#cb105-6" aria-hidden="true" tabindex="-1"></a>chmod <span class="sc">+</span>w <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 修改权限</span></span>
<span id="cb105-7"><a href="r-deve.html#cb105-7" aria-hidden="true" tabindex="-1"></a>sudo vim <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 编辑</span></span>
<span id="cb105-8"><a href="r-deve.html#cb105-8" aria-hidden="true" tabindex="-1"></a><span class="co"># root ALL=(ALL) ALL 该行，在下面添加 shiny ALL=(ALL:ALL) ALL</span></span>
<span id="cb105-9"><a href="r-deve.html#cb105-9" aria-hidden="true" tabindex="-1"></a>chmod <span class="sc">-</span>w <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 收回权限</span></span>
<span id="cb105-10"><a href="r-deve.html#cb105-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-11"><a href="r-deve.html#cb105-11" aria-hidden="true" tabindex="-1"></a>su <span class="sc">-</span> shiny <span class="co"># 切换到shiny用户</span></span>
<span id="cb105-12"><a href="r-deve.html#cb105-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-13"><a href="r-deve.html#cb105-13" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装R</span></span>
<span id="cb105-14"><a href="r-deve.html#cb105-14" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get update</span>
<span id="cb105-15"><a href="r-deve.html#cb105-15" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install r<span class="sc">-</span>base</span>
<span id="cb105-16"><a href="r-deve.html#cb105-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-17"><a href="r-deve.html#cb105-17" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装shiny包</span></span>
<span id="cb105-18"><a href="r-deve.html#cb105-18" aria-hidden="true" tabindex="-1"></a>sudo su <span class="sc">-</span>c <span class="st">"R -e </span><span class="sc">\"</span><span class="st">install.packages('shiny',repos='https://cran.rstudio.com/')</span><span class="sc">\"</span><span class="st">"</span></span>
<span id="cb105-19"><a href="r-deve.html#cb105-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb105-20"><a href="r-deve.html#cb105-20" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装shiny server</span></span>
<span id="cb105-21"><a href="r-deve.html#cb105-21" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install gdebi<span class="sc">-</span>core</span>
<span id="cb105-22"><a href="r-deve.html#cb105-22" aria-hidden="true" tabindex="-1"></a>wget https<span class="sc">:</span><span class="er">//</span>download3.rstudio.org<span class="sc">/</span>ubuntu<span class="fl">-14.04</span><span class="sc">/</span>x86_64<span class="sc">/</span>shiny<span class="sc">-</span>server<span class="dv">-1</span>.<span class="dv">5</span>.<span class="fl">17.973</span><span class="sc">-</span>amd64.deb</span>
<span id="cb105-23"><a href="r-deve.html#cb105-23" aria-hidden="true" tabindex="-1"></a>sudo gdebi shiny<span class="sc">-</span>server<span class="dv">-1</span>.<span class="dv">5</span>.<span class="fl">17.973</span><span class="sc">-</span>amd64.deb</span></code></pre></div>
<p>安装shiny-server的时候参考官方最新的指导页面，<a href="https://www.rstudio.com/products/shiny/download-server/ubuntu/">点击访问</a>。</p>
<p>日志所在目录为：<code>/var/log/shiny-server</code>。需要修改配置文件，保证记录运行日志，否则可能找不到报错提示。</p>
<div class="sourceCode" id="cb106"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb106-1"><a href="r-deve.html#cb106-1" aria-hidden="true" tabindex="-1"></a>vi <span class="sc">/</span>etc<span class="sc">/</span>shiny<span class="sc">-</span>server<span class="sc">/</span>shiny<span class="sc">-</span>server.conf</span>
<span id="cb106-2"><a href="r-deve.html#cb106-2" aria-hidden="true" tabindex="-1"></a>preserve_logs true; <span class="co"># 在最上面添加这行</span></span></code></pre></div>
<p>软件所在目录为：<code>/srv/shiny-server</code></p>
<p>安装完成后在浏览器输入<code>IP:3838</code>就能访问到示例界面了。默认的端口是<code>3838</code>端口，需要其他端口的需要自行调整。</p>
<p>基本管理：</p>
<div class="sourceCode" id="cb107"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb107-1"><a href="r-deve.html#cb107-1" aria-hidden="true" tabindex="-1"></a><span class="do">## 重启</span></span>
<span id="cb107-2"><a href="r-deve.html#cb107-2" aria-hidden="true" tabindex="-1"></a>sudo systemctl restart shiny<span class="sc">-</span>server</span>
<span id="cb107-3"><a href="r-deve.html#cb107-3" aria-hidden="true" tabindex="-1"></a><span class="do">##查看状态 </span></span>
<span id="cb107-4"><a href="r-deve.html#cb107-4" aria-hidden="true" tabindex="-1"></a>sudo systemctl status shiny<span class="sc">-</span>server </span>
<span id="cb107-5"><a href="r-deve.html#cb107-5" aria-hidden="true" tabindex="-1"></a><span class="do">##开启 </span></span>
<span id="cb107-6"><a href="r-deve.html#cb107-6" aria-hidden="true" tabindex="-1"></a>sudo systemctl start shiny<span class="sc">-</span>server </span>
<span id="cb107-7"><a href="r-deve.html#cb107-7" aria-hidden="true" tabindex="-1"></a><span class="do">##停止 </span></span>
<span id="cb107-8"><a href="r-deve.html#cb107-8" aria-hidden="true" tabindex="-1"></a>sudo systemctl stop shiny<span class="sc">-</span>server </span></code></pre></div>
</div>
<div id="ubuntu安装devtools" class="section level3" number="4.4.3">
<h3>
<span class="header-section-number">4.4.3</span> Ubuntu安装devtools<a class="anchor" aria-label="anchor" href="#ubuntu%E5%AE%89%E8%A3%85devtools"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb108"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb108-1"><a href="r-deve.html#cb108-1" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install <span class="sc">-</span>y libxml2<span class="sc">-</span>dev libcurl4<span class="sc">-</span>openssl<span class="sc">-</span>dev libssl<span class="sc">-</span>devv</span></code></pre></div>
<p>最重要的是上面这一步，剩下的差啥装啥就OK。</p>
<p>安装<code>proj</code>这个包：</p>
<div class="sourceCode" id="cb109"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb109-1"><a href="r-deve.html#cb109-1" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install libproj<span class="sc">-</span>dev</span>
<span id="cb109-2"><a href="r-deve.html#cb109-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb109-3"><a href="r-deve.html#cb109-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"RcppEigen"</span>, <span class="at">INSTALL_opts =</span> <span class="st">'--no-lock'</span>)</span></code></pre></div>
<p>按照依赖库：</p>
<div class="sourceCode" id="cb110"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb110-1"><a href="r-deve.html#cb110-1" aria-hidden="true" tabindex="-1"></a>wget ftp<span class="sc">:</span><span class="er">//</span>xmlsoft.org<span class="sc">/</span>libxml2<span class="sc">/</span>libxml2<span class="dv">-2</span>.<span class="dv">9</span>.<span class="fl">2.</span>tar.gz</span>
<span id="cb110-2"><a href="r-deve.html#cb110-2" aria-hidden="true" tabindex="-1"></a>tar <span class="sc">-</span>xvf libxml2<span class="dv">-2</span>.<span class="dv">9</span>.<span class="fl">2.</span>tar.gz</span>
<span id="cb110-3"><a href="r-deve.html#cb110-3" aria-hidden="true" tabindex="-1"></a>cd libxml2<span class="dv">-2</span>.<span class="fl">9.2</span></span>
<span id="cb110-4"><a href="r-deve.html#cb110-4" aria-hidden="true" tabindex="-1"></a>.<span class="sc">/</span>configure <span class="sc">--</span>prefix<span class="ot">=</span><span class="er">/</span>usr <span class="sc">--</span>disable<span class="sc">-</span>static <span class="sc">--</span>with<span class="sc">-</span>history <span class="sc">&amp;&amp;</span> make</span>
<span id="cb110-5"><a href="r-deve.html#cb110-5" aria-hidden="true" tabindex="-1"></a>sudo make install</span></code></pre></div>
</div>
<div id="上传文件大小限制" class="section level3" number="4.4.4">
<h3>
<span class="header-section-number">4.4.4</span> 上传文件大小限制<a class="anchor" aria-label="anchor" href="#%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E5%A4%A7%E5%B0%8F%E9%99%90%E5%88%B6"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb111"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/base/options.html">options</a></span><span class="op">(</span>shiny.maxRequestSize<span class="op">=</span><span class="fl">1000</span><span class="op">*</span><span class="fl">1024</span><span class="op">^</span><span class="fl">2</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="美化rshiny" class="section level2" number="4.5">
<h2>
<span class="header-section-number">4.5</span> 美化RShiny<a class="anchor" aria-label="anchor" href="#%E7%BE%8E%E5%8C%96rshiny"><i class="fas fa-link"></i></a>
</h2>
<div id="htmltools包的使用" class="section level3" number="4.5.1">
<h3>
<span class="header-section-number">4.5.1</span> htmltools包的使用<a class="anchor" aria-label="anchor" href="#htmltools%E5%8C%85%E7%9A%84%E4%BD%BF%E7%94%A8"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb112"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/rstudio/htmltools">htmltools</a></span><span class="op">)</span>
<span class="va">tag</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">div</a></span><span class="op">(</span><span class="st">"Hello World"</span><span class="op">)</span>
<span class="va">tag</span></code></pre></div>
<p>查看HTML下的所有tags：</p>
<div class="sourceCode" id="cb113"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb113-1"><a href="r-deve.html#cb113-1" aria-hidden="true" tabindex="-1"></a>htmltools<span class="sc">::</span>tags<span class="sc">$</span></span></code></pre></div>
<p>在R里面使用tags的两种方法：</p>
<div class="sourceCode" id="cb114"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># good</span>
<span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">h1</a></span><span class="op">(</span><span class="st">"This is a title"</span><span class="op">)</span>

<span class="co"># correct but not necessary</span>
<span class="va">tags</span><span class="op">$</span><span class="fu">h1</span><span class="op">(</span><span class="st">"This is a title"</span><span class="op">)</span></code></pre></div>
<p>含有大量tags时候的写法：</p>
<div class="sourceCode" id="cb115"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># Better</span>
<span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/withTags.html">withTags</a></span><span class="op">(</span>
  <span class="fu">nav</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">div</a></span><span class="op">(</span><span class="op">)</span>, <span class="fu">ul</span><span class="op">(</span><span class="fu">li</span><span class="op">(</span><span class="op">)</span>, <span class="fu">li</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>
<span class="op">)</span>

<span class="co"># instead of </span>
<span class="va">tags</span><span class="op">$</span><span class="fu">nav</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">div</a></span><span class="op">(</span><span class="op">)</span>, <span class="va">tags</span><span class="op">$</span><span class="fu">ul</span><span class="op">(</span><span class="va">tags</span><span class="op">$</span><span class="fu">li</span><span class="op">(</span><span class="op">)</span>, <span class="va">tags</span><span class="op">$</span><span class="fu">li</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">)</span>

<span class="co"># good</span>
<span class="va">tag_list_1</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/tagList.html">tagList</a></span><span class="op">(</span>
  <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">p</a></span><span class="op">(</span><span class="st">"Some text"</span><span class="op">)</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">div</a></span><span class="op">(</span><span class="st">"Content"</span><span class="op">)</span>
<span class="op">)</span>

<span class="fu"><a href="https://rdrr.io/r/utils/str.html">str</a></span><span class="op">(</span><span class="va">tag_list_1</span><span class="op">)</span></code></pre></div>
<p>如何定义新的tag：</p>
<div class="sourceCode" id="cb116"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">customTag</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">tag</a></span><span class="op">(</span>
  <span class="st">"test"</span>, 
  <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span>class <span class="op">=</span> <span class="st">"test"</span>, <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">p</a></span><span class="op">(</span><span class="st">"Custom Tag"</span><span class="op">)</span><span class="op">)</span>
<span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/utils/str.html">str</a></span><span class="op">(</span><span class="va">customTag</span><span class="op">)</span></code></pre></div>
<p>将HTML代码转换成R代码：<a href="https://github.com/alandipert/html2r" class="uri">https://github.com/alandipert/html2r</a></p>
<p>如何玩转R里面的tag：</p>
<div class="sourceCode" id="cb117"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># create the tag</span>
<span class="va">myTag</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">div</a></span><span class="op">(</span>
  class <span class="op">=</span> <span class="st">"divclass"</span>, 
  id <span class="op">=</span> <span class="st">"first"</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">h1</a></span><span class="op">(</span><span class="st">"My first child!"</span><span class="op">)</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/htmltools/man/builder.html">span</a></span><span class="op">(</span>class <span class="op">=</span> <span class="st">"child"</span>, id <span class="op">=</span> <span class="st">"baby"</span>, <span class="st">"Crying"</span><span class="op">)</span>
<span class="op">)</span>
<span class="co"># access its name</span>
<span class="co"># myTag$name</span>
<span class="co"># access its attributes (id and class)</span>
<span class="co"># myTag$attribs</span>
<span class="co"># access children (returns a list of 2 elements)</span>
<span class="co"># myTag$children</span>
<span class="co"># access its class</span>
<span class="fu"><a href="https://rdrr.io/r/utils/str.html">str</a></span><span class="op">(</span><span class="va">myTag</span><span class="op">)</span></code></pre></div>
<p>如何修改子tag的class：</p>
<div class="sourceCode" id="cb118"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">myTag</span><span class="op">$</span><span class="va">children</span><span class="op">[[</span><span class="fl">2</span><span class="op">]</span><span class="op">]</span><span class="op">$</span><span class="va">attribs</span><span class="op">$</span><span class="va">class</span> <span class="op">&lt;-</span> <span class="st">"adult"</span>
<span class="va">myTag</span></code></pre></div>
<p>管理CSS的三种方式：</p>
<ul>
<li>
<strong>External</strong> CSS file <code>tags$link(rel = "stylesheet", type="text/css", href="www/style.css")</code>.</li>
<li>
<strong>Internal</strong> CSS <code>tags$head(tags$style("p {color: red;}"))</code>.</li>
<li>
<strong>Inline</strong> CSS <code><a href="https://rdrr.io/pkg/htmltools/man/builder.html">p(style = "color:red;", "Red text")</a></code>.</li>
</ul>
<p>CSS选择器：</p>
<div class="sourceCode" id="cb119"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb119-1"><a href="r-deve.html#cb119-1" aria-hidden="true" tabindex="-1"></a>selector {</span>
<span id="cb119-2"><a href="r-deve.html#cb119-2" aria-hidden="true" tabindex="-1"></a>  property<span class="sc">:</span> value;</span>
<span id="cb119-3"><a href="r-deve.html#cb119-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb119-4"><a href="r-deve.html#cb119-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb119-5"><a href="r-deve.html#cb119-5" aria-hidden="true" tabindex="-1"></a><span class="co"># 如</span></span>
<span id="cb119-6"><a href="r-deve.html#cb119-6" aria-hidden="true" tabindex="-1"></a>p {</span>
<span id="cb119-7"><a href="r-deve.html#cb119-7" aria-hidden="true" tabindex="-1"></a>  color<span class="sc">:</span> red;  </span>
<span id="cb119-8"><a href="r-deve.html#cb119-8" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<p>多个选择器：</p>
<div class="sourceCode" id="cb120"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb120-1"><a href="r-deve.html#cb120-1" aria-hidden="true" tabindex="-1"></a>p, div {</span>
<span id="cb120-2"><a href="r-deve.html#cb120-2" aria-hidden="true" tabindex="-1"></a>  color<span class="sc">:</span> red;  </span>
<span id="cb120-3"><a href="r-deve.html#cb120-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb120-4"><a href="r-deve.html#cb120-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb120-5"><a href="r-deve.html#cb120-5" aria-hidden="true" tabindex="-1"></a><span class="co"># 如</span></span>
<span id="cb120-6"><a href="r-deve.html#cb120-6" aria-hidden="true" tabindex="-1"></a>ui <span class="ot">&lt;-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb120-7"><a href="r-deve.html#cb120-7" aria-hidden="true" tabindex="-1"></a>  tags<span class="sc">$</span><span class="fu">style</span>(</span>
<span id="cb120-8"><a href="r-deve.html#cb120-8" aria-hidden="true" tabindex="-1"></a>    <span class="st">"p, div {</span></span>
<span id="cb120-9"><a href="r-deve.html#cb120-9" aria-hidden="true" tabindex="-1"></a><span class="st">      color: red;</span></span>
<span id="cb120-10"><a href="r-deve.html#cb120-10" aria-hidden="true" tabindex="-1"></a><span class="st">    }"</span></span>
<span id="cb120-11"><a href="r-deve.html#cb120-11" aria-hidden="true" tabindex="-1"></a>  ),</span>
<span id="cb120-12"><a href="r-deve.html#cb120-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">p</span>(<span class="st">"Hello World"</span>),</span>
<span id="cb120-13"><a href="r-deve.html#cb120-13" aria-hidden="true" tabindex="-1"></a>  <span class="fu">div</span>(<span class="st">"A block"</span>)</span>
<span id="cb120-14"><a href="r-deve.html#cb120-14" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>通过class和id进行选择：</p>
<div class="sourceCode" id="cb121"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb121-1"><a href="r-deve.html#cb121-1" aria-hidden="true" tabindex="-1"></a>.first<span class="sc">-</span>p {</span>
<span id="cb121-2"><a href="r-deve.html#cb121-2" aria-hidden="true" tabindex="-1"></a>  color<span class="sc">:</span> red;  </span>
<span id="cb121-3"><a href="r-deve.html#cb121-3" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb121-4"><a href="r-deve.html#cb121-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-5"><a href="r-deve.html#cb121-5" aria-hidden="true" tabindex="-1"></a><span class="co">#element {</span></span>
<span id="cb121-6"><a href="r-deve.html#cb121-6" aria-hidden="true" tabindex="-1"></a>  color<span class="sc">:</span> red;</span>
<span id="cb121-7"><a href="r-deve.html#cb121-7" aria-hidden="true" tabindex="-1"></a><span class="er">}</span></span>
<span id="cb121-8"><a href="r-deve.html#cb121-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-9"><a href="r-deve.html#cb121-9" aria-hidden="true" tabindex="-1"></a>ui <span class="ot">&lt;-</span> <span class="fu">fluidPage</span>(</span>
<span id="cb121-10"><a href="r-deve.html#cb121-10" aria-hidden="true" tabindex="-1"></a>  tags<span class="sc">$</span><span class="fu">style</span>(</span>
<span id="cb121-11"><a href="r-deve.html#cb121-11" aria-hidden="true" tabindex="-1"></a>    <span class="st">".first-p {</span></span>
<span id="cb121-12"><a href="r-deve.html#cb121-12" aria-hidden="true" tabindex="-1"></a><span class="st">      color: red;</span></span>
<span id="cb121-13"><a href="r-deve.html#cb121-13" aria-hidden="true" tabindex="-1"></a><span class="st">    }</span></span>
<span id="cb121-14"><a href="r-deve.html#cb121-14" aria-hidden="true" tabindex="-1"></a><span class="st">    #element {</span></span>
<span id="cb121-15"><a href="r-deve.html#cb121-15" aria-hidden="true" tabindex="-1"></a><span class="st">      color: red;</span></span>
<span id="cb121-16"><a href="r-deve.html#cb121-16" aria-hidden="true" tabindex="-1"></a><span class="st">    }</span></span>
<span id="cb121-17"><a href="r-deve.html#cb121-17" aria-hidden="true" tabindex="-1"></a><span class="st">    "</span></span>
<span id="cb121-18"><a href="r-deve.html#cb121-18" aria-hidden="true" tabindex="-1"></a>  ),</span>
<span id="cb121-19"><a href="r-deve.html#cb121-19" aria-hidden="true" tabindex="-1"></a>  <span class="fu">p</span>(<span class="at">class =</span> <span class="st">"first-p"</span>, <span class="st">"Hello World"</span>),</span>
<span id="cb121-20"><a href="r-deve.html#cb121-20" aria-hidden="true" tabindex="-1"></a>  <span class="fu">p</span>(<span class="st">"Another text"</span>),</span>
<span id="cb121-21"><a href="r-deve.html#cb121-21" aria-hidden="true" tabindex="-1"></a>  <span class="fu">div</span>(<span class="at">id =</span> <span class="st">"element"</span>, <span class="st">"A block"</span>)</span>
<span id="cb121-22"><a href="r-deve.html#cb121-22" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>如何改变某个字的属性呢？</p>
<div class="sourceCode" id="cb122"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb122-1"><a href="r-deve.html#cb122-1" aria-hidden="true" tabindex="-1"></a><span class="sc">&lt;</span>p<span class="sc">&gt;</span>Hello <span class="sc">&lt;</span>span class<span class="ot">=</span><span class="st">"custom"</span><span class="sc">&gt;</span>World<span class="sc">&lt;</span><span class="er">/</span>span<span class="sc">&gt;!</span> What<span class="st">'s up?&lt;/p&gt;</span></span>
<span id="cb122-2"><a href="r-deve.html#cb122-2" aria-hidden="true" tabindex="-1"></a><span class="st">.custom {</span></span>
<span id="cb122-3"><a href="r-deve.html#cb122-3" aria-hidden="true" tabindex="-1"></a><span class="st">  color: red;</span></span>
<span id="cb122-4"><a href="r-deve.html#cb122-4" aria-hidden="true" tabindex="-1"></a><span class="st">}</span></span></code></pre></div>
<p>class一致的时候怎么选择：先选择tag</p>
<div class="sourceCode" id="cb123"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb123-1"><a href="r-deve.html#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="sc">&lt;</span>li class<span class="ot">=</span><span class="st">"nav-item"</span><span class="sc">&gt;</span></span>
<span id="cb123-2"><a href="r-deve.html#cb123-2" aria-hidden="true" tabindex="-1"></a>  <span class="er">&lt;</span>a class<span class="ot">=</span><span class="st">"nav-link"</span> href<span class="ot">=</span><span class="st">"#"</span><span class="sc">&gt;</span>Link<span class="sc">&lt;</span><span class="er">/</span>a<span class="sc">&gt;</span></span>
<span id="cb123-3"><a href="r-deve.html#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="er">&lt;/</span>li<span class="sc">&gt;</span></span>
<span id="cb123-4"><a href="r-deve.html#cb123-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-5"><a href="r-deve.html#cb123-5" aria-hidden="true" tabindex="-1"></a><span class="er">&lt;</span>a class<span class="ot">=</span><span class="st">"nav-link"</span> href<span class="ot">=</span><span class="st">"#"</span><span class="sc">&gt;</span>Link<span class="sc">&lt;</span><span class="er">/</span>a<span class="sc">&gt;</span></span>
<span id="cb123-6"><a href="r-deve.html#cb123-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-7"><a href="r-deve.html#cb123-7" aria-hidden="true" tabindex="-1"></a>li a {</span>
<span id="cb123-8"><a href="r-deve.html#cb123-8" aria-hidden="true" tabindex="-1"></a>  color<span class="sc">:</span> red;</span>
<span id="cb123-9"><a href="r-deve.html#cb123-9" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div>
<div class="sourceCode" id="cb124"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co">### RUN ### </span>
<span class="co"># OSUICode::run_example( </span>
<span class="co">#  "css/selector-nested/ex1", </span>
<span class="co">#   package = "OSUICode" </span>
<span class="co"># ) </span>

<span class="co">### APP CODE ### </span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://shiny.rstudio.com/">shiny</a></span><span class="op">)</span>

<span class="va">ui</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/navbarPage.html">navbarPage</a></span><span class="op">(</span>
  <span class="st">"App Title"</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span>
    <span class="st">"Plot"</span>,
    <span class="va">tags</span><span class="op">$</span><span class="fu">style</span><span class="op">(</span>
      <span class="st">"li a {
        font-size: 20px;
        font-weight: bold;
      }
    "</span>
    <span class="op">)</span>,
    <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabsetPanel.html">tabsetPanel</a></span><span class="op">(</span>
      <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span><span class="st">"Plot"</span><span class="op">)</span>,
      <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span><span class="st">"Summary"</span><span class="op">)</span>,
      <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span><span class="st">"Table"</span><span class="op">)</span>
    <span class="op">)</span>
  <span class="op">)</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span><span class="st">"Summary"</span><span class="op">)</span>,
  <span class="fu"><a href="https://rdrr.io/pkg/shiny/man/tabPanel.html">tabPanel</a></span><span class="op">(</span><span class="st">"Table"</span><span class="op">)</span>
<span class="op">)</span>

<span class="va">server</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">input</span>, <span class="va">output</span><span class="op">)</span> <span class="op">{</span><span class="op">}</span>

<span class="fu"><a href="https://rdrr.io/pkg/shiny/man/shinyApp.html">shinyApp</a></span><span class="op">(</span><span class="va">ui</span>, <span class="va">server</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="golem包开发产品级shiny" class="section level2" number="4.6">
<h2>
<span class="header-section-number">4.6</span> golem包开发产品级Shiny<a class="anchor" aria-label="anchor" href="#golem%E5%8C%85%E5%BC%80%E5%8F%91%E4%BA%A7%E5%93%81%E7%BA%A7shiny"><i class="fas fa-link"></i></a>
</h2>
<p>golem的内核思想是<code>每个Rshiny应用都应该是个R包</code>。</p>
<div id="什么是产品级的shiny-app" class="section level3" number="4.6.1">
<h3>
<span class="header-section-number">4.6.1</span> 什么是产品级的shiny app<a class="anchor" aria-label="anchor" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E4%BA%A7%E5%93%81%E7%BA%A7%E7%9A%84shiny-app"><i class="fas fa-link"></i></a>
</h3>
<ul>
<li>有metadata</li>
</ul>
<p>app的名字、版本、功能、报错时的联系信息等。这个部分的内容相当于R包里面的<code>DESCRIPTIONM</code>。</p>
<ul>
<li>没有依赖关系上的报错</li>
</ul>
<p>把整个shiny app中需要用到的包写成R包里面的<code>DESCRIPTIONM</code>格式，把需要用到的函数写成R包里面的<code>NAMESPACE</code>文件。这样就能避免各种各样的报错。</p>
<ul>
<li>整个app划分成不同的function模块</li>
</ul>
<p>避免将app的左右函数都写在一个文档里面，那就将这些function分开写，并像R包那样放在<code>R</code>整个文件夹内。</p>
<ul>
<li>有文档手册
<ul>
<li>
<code>README</code>文件：放在根目录下，说明如何安装这个app以及如何使用这个app。</li>
<li>
<code>Vignettes</code>：一个比<code>README</code>文件更长的文档，从细节上说明如何使用这个app。</li>
<li>函数文档：和写R包一样，app里面的每个函数都应该有对应的文档。</li>
</ul>
</li>
<li>测试运行。</li>
</ul>
<p><strong>Nothing should go to production without being tested. Nothing.</strong></p>
<p>把shiny app写成R包的构造方便进行测试，不如用<code>testthat</code>进行测试。</p>
</div>
</div>
<div id="如何在模块间传递数据" class="section level2" number="4.7">
<h2>
<span class="header-section-number">4.7</span> 如何在模块间传递数据<a class="anchor" aria-label="anchor" href="#%E5%A6%82%E4%BD%95%E5%9C%A8%E6%A8%A1%E5%9D%97%E9%97%B4%E4%BC%A0%E9%80%92%E6%95%B0%E6%8D%AE"><i class="fas fa-link"></i></a>
</h2>
<ul>
<li>通过reactive返回数据</li>
<li>
<a href="https://shiny.rstudio.com/reference/shiny/latest/reactiveValues.html">reactiveValues()</a>构造全局变量</li>
<li>创造R6对象进行传递。</li>
</ul>
<div id="golem结构" class="section level3" number="4.7.1">
<h3>
<span class="header-section-number">4.7.1</span> golem结构<a class="anchor" aria-label="anchor" href="#golem%E7%BB%93%E6%9E%84"><i class="fas fa-link"></i></a>
</h3>
<p>golem的底层概念就是每个shiny app都是个R包。</p>
<p>创建golem对象：</p>
<div class="sourceCode" id="cb125"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">golem</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/golem/man/create_golem.html">create_golem</a></span><span class="op">(</span><span class="st">"shiny4RefPlantNLR"</span><span class="op">)</span></code></pre></div>
<p>查看结构：</p>
<div class="sourceCode" id="cb126"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">fs</span><span class="fu">::</span><span class="fu"><a href="http://fs.r-lib.org/reference/dir_tree.html">dir_tree</a></span><span class="op">(</span><span class="st">"shiny4RefPlantNLR"</span><span class="op">)</span></code></pre></div>
<div class="sourceCode" id="cb127"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb127-1"><a href="r-deve.html#cb127-1" aria-hidden="true" tabindex="-1"></a>shiny4RefPlantNLR</span>
<span id="cb127-2"><a href="r-deve.html#cb127-2" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> DESCRIPTION </span>
<span id="cb127-3"><a href="r-deve.html#cb127-3" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> dev</span>
<span id="cb127-4"><a href="r-deve.html#cb127-4" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> 01_start.R</span>
<span id="cb127-5"><a href="r-deve.html#cb127-5" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> 02_dev.R</span>
<span id="cb127-6"><a href="r-deve.html#cb127-6" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> 03_deploy.R</span>
<span id="cb127-7"><a href="r-deve.html#cb127-7" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   \<span class="sc">--</span> run_dev.R</span>
<span id="cb127-8"><a href="r-deve.html#cb127-8" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> inst</span>
<span id="cb127-9"><a href="r-deve.html#cb127-9" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> app</span>
<span id="cb127-10"><a href="r-deve.html#cb127-10" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="er">|</span>   \<span class="sc">--</span> www</span>
<span id="cb127-11"><a href="r-deve.html#cb127-11" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="er">|</span>       \<span class="sc">--</span> favicon.ico</span>
<span id="cb127-12"><a href="r-deve.html#cb127-12" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   \<span class="sc">--</span> golem<span class="sc">-</span>config.yml</span>
<span id="cb127-13"><a href="r-deve.html#cb127-13" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> man</span>
<span id="cb127-14"><a href="r-deve.html#cb127-14" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   \<span class="sc">--</span> run_app.Rd</span>
<span id="cb127-15"><a href="r-deve.html#cb127-15" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> NAMESPACE</span>
<span id="cb127-16"><a href="r-deve.html#cb127-16" aria-hidden="true" tabindex="-1"></a><span class="sc">+--</span> R</span>
<span id="cb127-17"><a href="r-deve.html#cb127-17" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> app_config.R</span>
<span id="cb127-18"><a href="r-deve.html#cb127-18" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> app_server.R</span>
<span id="cb127-19"><a href="r-deve.html#cb127-19" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   <span class="sc">+--</span> app_ui.R</span>
<span id="cb127-20"><a href="r-deve.html#cb127-20" aria-hidden="true" tabindex="-1"></a><span class="sc">|</span>   \<span class="sc">--</span> run_app.R</span>
<span id="cb127-21"><a href="r-deve.html#cb127-21" aria-hidden="true" tabindex="-1"></a>\<span class="sc">--</span> shiny4RefPlantNLR.Rproj</span></code></pre></div>
<ul>
<li>DESCRIPTION</li>
</ul>
<p>含有各种metadata,比如谁写的app，版本，app目的是啥，报错应该联系谁等等。这个文件是由<code>dev/01_start.R</code>这个函数自动生成的。</p>
<ul>
<li>NAMESPACE</li>
</ul>
<p>这个文件是绝对不能手动编辑的！！！包含的信息有：哪个包的哪个函数被导入，哪个功能被export。如何产生这个文件呢：</p>
<div class="sourceCode" id="cb128"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">devtools</span><span class="fu">::</span><span class="fu"><a href="https://devtools.r-lib.org//reference/document.html">document</a></span><span class="op">(</span><span class="op">)</span>
<span class="co">## OR</span>
<span class="fu">golem</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/golem/man/document_and_reload.html">document_and_reload</a></span><span class="op">(</span><span class="op">)</span></code></pre></div>
<ul>
<li>
<em>R</em>文件夹</li>
</ul>
<p>用于存放核心函数。其实只要是<code>.R</code>结尾的文件都应该放在这个文件夹里面。那为什么在<code>dev/</code>这个文件夹下面还有<code>.R</code>结尾的文件呢？虽然是<code>.R</code>结尾的文件，但是他们并不是核心函数吗，只是开发过程中需要用到的信息。</p>
<p>特别需要注意的是这个文件夹下面不能有任何子文件夹！！！</p>
</div>
</div>
<div id="网页工具shiny4qpcr使用说明" class="section level2" number="4.8">
<h2>
<span class="header-section-number">4.8</span> 网页工具shiny4qPCR使用说明<a class="anchor" aria-label="anchor" href="#%E7%BD%91%E9%A1%B5%E5%B7%A5%E5%85%B7shiny4qpcr%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><i class="fas fa-link"></i></a>
</h2>
<p>工具地址：<a href="http://106.55.94.9:3838/shiny4qPCR/" class="uri">http://106.55.94.9:3838/shiny4qPCR/</a></p>
<div id="反转录计算" class="section level3" number="4.8.1">
<h3>
<span class="header-section-number">4.8.1</span> 反转录计算<a class="anchor" aria-label="anchor" href="#%E5%8F%8D%E8%BD%AC%E5%BD%95%E8%AE%A1%E7%AE%97"><i class="fas fa-link"></i></a>
</h3>
<div class="inline-figure">反转录使用的试剂盒为全式金的TransScript® All-in-One First-Strand cDNA Synthesis SuperMix for qPCR(One-Step gDNA Removal)。其反转录步骤为：
<img src="figures/shiny4qPCR/1.png">
</div>
<p>在进行计算是只需要上传Nano Drop2000的下机数据，再选择需要反转的体积即可。计算完成后返回的数据是求均值后的数据，即对每个样品求均值，然后继续计算，组后返回结果。
Nano Drop2000的下机数据样式点击下载示例数据即可。</p>
</div>
<div id="标曲计算" class="section level3" number="4.8.2">
<h3>
<span class="header-section-number">4.8.2</span> 标曲计算<a class="anchor" aria-label="anchor" href="#%E6%A0%87%E6%9B%B2%E8%AE%A1%E7%AE%97"><i class="fas fa-link"></i></a>
</h3>
<div class="inline-figure">标曲计算需要一个Excel文件，找个Excel文件中需要有三个sheet，三个sheet的数据分别是：
- Roche qPCR仪器下机数据；
- 基因布局图：告诉程序每个孔里面的基因叫什么名字；
<img src="figures/shiny4qPCR/2.png">
</div>
<ul>
<li>浓度布局图：告诉程序每个孔里面的物质的相对浓度是多少。</li>
</ul>
<div class="inline-figure"><img src="figures/shiny4qPCR/3.png"></div>
<div class="inline-figure">
<img src="figures/shiny4qPCR/4.png">
需要注意的地方：
- 空值填充时是用同一基因同一浓度下的均值进行填充。</div>
</div>
<div id="计算表达量标曲法" class="section level3" number="4.8.3">
<h3>
<span class="header-section-number">4.8.3</span> 计算表达量（标曲法）<a class="anchor" aria-label="anchor" href="#%E8%AE%A1%E7%AE%97%E8%A1%A8%E8%BE%BE%E9%87%8F%E6%A0%87%E6%9B%B2%E6%B3%95"><i class="fas fa-link"></i></a>
</h3>
<p>用标曲法计算表达量时，也只需要上传一个文件即可（下载示例数据查看样式）。四个文件分别为：
- qPCR下机数据；
- 处理布局图：告诉程序每个孔分别属于什么处理；</p>
<div class="inline-figure"><img src="figures/shiny4qPCR/5.png"></div>
<ul>
<li>基因布局图：告诉程序每个孔分别是什么基因（基因名称严格区分大小写）；</li>
</ul>
<div class="inline-figure"><img src="figures/shiny4qPCR/6.png"></div>
<ul>
<li>基因标曲信息：前面输出的标曲信息。</li>
</ul>
<div class="inline-figure"><img src="figures/shiny4qPCR/7.png"></div>
<p>需要注意的地方：
- 基因名称严格区分大小写；
- <strong>前三个表格的第一列必须是Batch</strong>，也就是告诉成程序你的数据属于第几批次，当有多次下机数据的时候，将多次下机数据合并在一个文件进行上传计算会方便很多。
- 程序默认使用内参基因对表达量进行校正，可以选择不校正。如果选择用内参基因进行校正，那就需要输入内参基因名称，必须严格区分大小写。</p>
</div>
<div id="计算表达量2-deltadeltac_t法" class="section level3" number="4.8.4">
<h3>
<span class="header-section-number">4.8.4</span> 计算表达量（2<span class="math inline">\(^{-\delta{\delta}C_t}\)</span>法）<a class="anchor" aria-label="anchor" href="#%E8%AE%A1%E7%AE%97%E8%A1%A8%E8%BE%BE%E9%87%8F2-deltadeltac_t%E6%B3%95"><i class="fas fa-link"></i></a>
</h3>
<p>使用2<span class="math inline">\(^{-\delta{\delta}C_t}\)</span>法计算表达量的输入数据和使用标曲法输入数据是一致的，唯一的差别在于使用2<span class="math inline">\(^{-\delta{\delta}C_t}\)</span>法不需要标曲数据，前三个表是一样的。</p>
</div>
<div id="差异表达统计" class="section level3" number="4.8.5">
<h3>
<span class="header-section-number">4.8.5</span> 差异表达统计<a class="anchor" aria-label="anchor" href="#%E5%B7%AE%E5%BC%82%E8%A1%A8%E8%BE%BE%E7%BB%9F%E8%AE%A1"><i class="fas fa-link"></i></a>
</h3>
<p>差异表达统计需要输入一个数据，也就是前面计算完表达量以后的数据文件。关键的有三列：
- Treatment：是什么处理
- Gene：是哪个基因
- Expression：对应的表达量</p>
<div class="inline-figure"><img src="figures/shiny4qPCR/8.png"></div>
<p>程序默认使用t-test进行统计检验，当有多个处理时可以选择Anova。统计检验水培默认的是0.95，可以选择0.99。
得到的结果只算统计分析的结果，没有包含原始数据。</p>

</div>
</div>
</div>



  <div class="chapter-nav">
<div class="prev"><a href="r-vis.html"><span class="header-section-number">3</span> 数据可视化</a></div>
<div class="next"><a href="python-base.html"><span class="header-section-number">5</span> Python基础</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
    <nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
      <ul class="nav navbar-nav">
<li>
<a class="nav-link" href="#r-deve"><span class="header-section-number">4</span> R语言开发</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#%E4%B8%80%E4%BA%9B%E8%B5%84%E6%BA%90"><span class="header-section-number">4.0.1</span> 一些资源</a></li></ul>
</li>
<li><a class="nav-link" href="#%E4%B8%80%E4%BA%9Btips"><span class="header-section-number">4.1</span> 一些Tips</a></li>
<li>
<a class="nav-link" href="#pac4xiang"><span class="header-section-number">4.2</span> R包pac4xiang的使用</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E5%AE%89%E8%A3%85"><span class="header-section-number">4.2.1</span> 安装</a></li>
<li><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"><span class="header-section-number">4.2.2</span> 使用方法</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#ggmotif"><span class="header-section-number">4.3</span> R包ggmotif的使用方法</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E4%BB%8Exml%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E7%BB%98%E5%9B%BE"><span class="header-section-number">4.3.1</span> 从XML文件提取结果并绘图</a></li>
<li><a class="nav-link" href="#%E4%BB%8Etxt%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96"><span class="header-section-number">4.3.2</span> 从txt文件提取结果并进行可视化</a></li>
<li><a class="nav-link" href="#%E4%B8%8Eggtree%E8%81%94%E7%94%A8"><span class="header-section-number">4.3.3</span> 与ggtree联用</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#r-shiny%E5%BC%80%E5%8F%91"><span class="header-section-number">4.4</span> R shiny开发</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#css%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99"><span class="header-section-number">4.4.1</span> CSS学习网站</a></li>
<li><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BArshiny-server"><span class="header-section-number">4.4.2</span> 服务器搭建Rshiny-server</a></li>
<li><a class="nav-link" href="#ubuntu%E5%AE%89%E8%A3%85devtools"><span class="header-section-number">4.4.3</span> Ubuntu安装devtools</a></li>
<li><a class="nav-link" href="#%E4%B8%8A%E4%BC%A0%E6%96%87%E4%BB%B6%E5%A4%A7%E5%B0%8F%E9%99%90%E5%88%B6"><span class="header-section-number">4.4.4</span> 上传文件大小限制</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#%E7%BE%8E%E5%8C%96rshiny"><span class="header-section-number">4.5</span> 美化RShiny</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#htmltools%E5%8C%85%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="header-section-number">4.5.1</span> htmltools包的使用</a></li></ul>
</li>
<li>
<a class="nav-link" href="#golem%E5%8C%85%E5%BC%80%E5%8F%91%E4%BA%A7%E5%93%81%E7%BA%A7shiny"><span class="header-section-number">4.6</span> golem包开发产品级Shiny</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E4%BA%A7%E5%93%81%E7%BA%A7%E7%9A%84shiny-app"><span class="header-section-number">4.6.1</span> 什么是产品级的shiny app</a></li></ul>
</li>
<li>
<a class="nav-link" href="#%E5%A6%82%E4%BD%95%E5%9C%A8%E6%A8%A1%E5%9D%97%E9%97%B4%E4%BC%A0%E9%80%92%E6%95%B0%E6%8D%AE"><span class="header-section-number">4.7</span> 如何在模块间传递数据</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#golem%E7%BB%93%E6%9E%84"><span class="header-section-number">4.7.1</span> golem结构</a></li></ul>
</li>
<li>
<a class="nav-link" href="#%E7%BD%91%E9%A1%B5%E5%B7%A5%E5%85%B7shiny4qpcr%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E"><span class="header-section-number">4.8</span> 网页工具shiny4qPCR使用说明</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E5%8F%8D%E8%BD%AC%E5%BD%95%E8%AE%A1%E7%AE%97"><span class="header-section-number">4.8.1</span> 反转录计算</a></li>
<li><a class="nav-link" href="#%E6%A0%87%E6%9B%B2%E8%AE%A1%E7%AE%97"><span class="header-section-number">4.8.2</span> 标曲计算</a></li>
<li><a class="nav-link" href="#%E8%AE%A1%E7%AE%97%E8%A1%A8%E8%BE%BE%E9%87%8F%E6%A0%87%E6%9B%B2%E6%B3%95"><span class="header-section-number">4.8.3</span> 计算表达量（标曲法）</a></li>
<li><a class="nav-link" href="#%E8%AE%A1%E7%AE%97%E8%A1%A8%E8%BE%BE%E9%87%8F2-deltadeltac_t%E6%B3%95"><span class="header-section-number">4.8.4</span> 计算表达量（2\(^{-\delta{\delta}C_t}\)法）</a></li>
<li><a class="nav-link" href="#%E5%B7%AE%E5%BC%82%E8%A1%A8%E8%BE%BE%E7%BB%9F%E8%AE%A1"><span class="header-section-number">4.8.5</span> 差异表达统计</a></li>
</ul>
</li>
</ul>

      <div class="book-extra">
        <ul class="list-unstyled">
<li><a id="book-source" href="https://github.com/lixiang117423/book4xiang/blob/master/4.R-deve.Rmd">View source <i class="fab fa-github"></i></a></li>
          <li><a id="book-edit" href="https://github.com/lixiang117423/book4xiang/edit/master/4.R-deve.Rmd">Edit this page <i class="fab fa-github"></i></a></li>
        </ul>
</div>
    </nav>
</div>

</div>
</div> <!-- .container -->

<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

  <div class="col-12 col-md-6 mt-3">
    <p>"<strong>小蓝哥的知识荒原</strong>" was written by 李详. It was last built on 2021年10月1日.</p>
  </div>

  <div class="col-12 col-md-6 mt-3">
    <p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
  </div>

</div></div>
</footer><!-- dynamically load mathjax for compatibility with self-contained --><script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script><script type="text/x-mathjax-config">const popovers = document.querySelectorAll('a.footnote-ref[data-toggle="popover"]');
for (let popover of popovers) {
  const div = document.createElement('div');
  div.setAttribute('style', 'position: absolute; top: 0, left:0; width:0, height:0, overflow: hidden; visibility: hidden;');
  div.innerHTML = popover.getAttribute('data-content');

  var has_math = div.querySelector("span.math");
  if (has_math) {
    document.body.appendChild(div);
    MathJax.Hub.Queue(["Typeset", MathJax.Hub, div]);
    MathJax.Hub.Queue(function() {
      popover.setAttribute('data-content', div.innerHTML);
      document.body.removeChild(div);
    })
  }
}
</script>
</body>
</html>
